package pro.cherish.knowledgedemovalid.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.cherish.knowledgedemovalid.entity.UserEntity;
import pro.cherish.knowledgedemovalid.entity.UserWraper;
import pro.cherish.knowledgedemovalid.mapper.UserMapper;

import javax.sql.DataSource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api("用户接口")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DataSource dataSource;

    @GetMapping("/userList")
    public Object getAllUserList(){
        List<UserEntity> allUserList = userMapper.getAllUserList();
        return allUserList;
    }
    @GetMapping("testUser01")
    public Object testUser01(UserEntity userEntity){
        // 或者使用isBlank方法，总之都是符合条件的情况下才运行程序
        if(userEntity.getUsername()!=null &&! "".equals(userEntity.getUsername().trim())){
            System.out.println(userEntity);
            return userEntity;
        }
        return "testUser01";
    }

    @GetMapping("testUser02")
    public Object testUser02(UserEntity userEntity){
        // 在不符合情况下结束程序运行，不执行后续程序
        if(userEntity.getUsername()==null ||  "".equals(userEntity.getUsername())){
            return "testUser02";
        }
        System.out.println(userEntity);
        return userEntity;
    }

    @GetMapping("testUser03")
    public void testUser03(UserEntity userEntity){
        // 不符合条件下直接抛出运行时异常
        if(userEntity.getUsername()==null ||  "".equals(userEntity.getUsername())){
            throw new RuntimeException("param is bad");
        }
    }


    // fixme 未起到效果,【暂未知】
    @ApiOperation(value = "测试方法基本类型参数校验",notes = "")
    @GetMapping("testUser04")
    public void testUser04(@Length(min = 2) String  userName){
        System.out.println(userName);
    }

    @ApiOperation(value = "测试方法参数为实体",notes = "")
    @GetMapping("testUser05")
    public void testUser05(@Valid UserEntity  userEntity){
        System.out.println(userEntity);
    }

    @ApiOperation(value = "测试方法参数为嵌套校验实体",notes = "")
    @GetMapping("testUser06")
    public void testUser06(@Validated UserWraper userWraper){
        System.out.println(userWraper);
    }

    @ApiOperation(value = "controller 方法中针对性处理异常",notes = "")
    @GetMapping("testUser07")
    public void testUser07(@Valid UserEntity  userEntity, BindingResult bindingResult){
        System.out.println(bindingResult);
        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            System.out.println(allErrors);
        }
        System.out.println(userEntity);
    }





}
